
//
//  Save.m
//  MyProject
//
//  Created by dllo on 15/11/9.
//  Copyright (c) 2015年 蓝鸥0825. All rights reserved.
//

#import "Save.h"

@implementation Save

+ (void)isHaveFolder {
    
    //      1. 找到沙盒路徑
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)lastObject];
    NSString *mineModel = [sandBoxPath stringByAppendingPathComponent:@"Caches"];
    
    //      創建一個文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    
    //      通过管理者调用的方法, 根据路径判断文件是否存在
    if (![manager fileExistsAtPath:mineModel]) {
        
        //  创建文件夹
        [manager createDirectoryAtPath:mineModel withIntermediateDirectories:YES attributes:nil error:nil];
        
    }
    NSLog(@"%@", mineModel);
}



+ (BOOL)isHaveModelInPlist:(tableViewModel *)model{
    
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"caches.plist"];
    
    //      反归档
    NSArray *modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    
    //      遍历
    for (tableViewModel *temp in modelArr) {
        if ([temp.title isEqualToString:model.title]) {
            return YES;
        }
    }
    return NO;
 
}




+ (void)saveModelToPlist:(tableViewModel *)model{
    
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"caches.plist"];
    
    //      直接归档会覆盖掉以前的值, 所以先进行反归档, 然后添进去之后在进行归档操作
    NSMutableArray *modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    
    //      遍历
    if (modelArr == nil) {
        //      如果为空, 先进行初始化
        modelArr = [NSMutableArray array];
    }
    [modelArr addObject:model];
    //      归档
    [NSKeyedArchiver archiveRootObject:modelArr toFile:docPath];
    
    
}


+ (void)deleteModel{
    
    NSArray *chaches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *chachesPath = chaches[0];
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL result1 = [manager removeItemAtPath:chachesPath error:nil];
    if (result1) {
        NSLog(@"成功");
    } else {
        NSLog(@"失败");
    }
    
}




@end
